using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace MiniPhysics
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        private GraphicsDeviceManager graphics;

        /** use these attributes to perform your tasks **/

        const int ball_size = 64;
        private List<Ball> balls;
        private Texture2D balltexture;

        protected void LoadScene()
        {
            Random r = new Random();
            int size = r.Next(42);
            balls = new List<Ball>(size);
            for (int i = 0; i <= size; i++)
            {
                int width = 50;
                Color maCouleur = new Color(r.Next(255), r.Next(255), r.Next(255));
                Rectangle rect = new Rectangle(r.Next(Window.ClientBounds.Width - 50), r.Next(Window.ClientBounds.Height - 50), width, width);
                Ball ball = new Ball(rect, this, maCouleur);
                while (this.superposition(ball))
                {
                    rect = new Rectangle(r.Next(Window.ClientBounds.Width - 50), r.Next(Window.ClientBounds.Height - 50), width, width);
                    ball = new Ball(rect, this, maCouleur);
                }
                balls.Add(ball);

            }
        }

        bool superposition(Ball ball)
        {
            bool v = false;
            foreach(Ball _ball in balls)
            {
                v = v || ball.do_collision_ball(_ball);
            }
            return v;
        }


        protected void UpdateScene(GameTime gameTime)
        {
            foreach (Ball ball in balls)
            {
                ball.push(new Vector2(0, 5000));
            }

            foreach (Ball ball in balls)
            {
                int place = 1;
                for (int i = place; i < balls.Count; i++)
                {
                    ball.do_collision_ball(balls[i]);
                }
                place++;
            }
            

            foreach (Ball ball in balls)
            {
                ball.update(gameTime);
                ball.do_collision_wall(Window.ClientBounds.Width, Window.ClientBounds.Height);
            }
        }

        protected void DrawScene()
        {
            foreach(Ball ball in balls)
            {
                ball.draw(balltexture);
            }
        }

        /** DO NOT modify the code below **/

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            base.Initialize();
        }

       
        protected override void LoadContent()
        {
            balltexture = Content.Load<Texture2D>("ball");
            LoadScene();
        }

   
        protected override void Update(GameTime gameTime)
        {
           
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            UpdateScene(gameTime);
            base.Update(gameTime);
        }

      
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.White);
            DrawScene();
            base.Draw(gameTime);
        }
    }
}
